package com.serivice.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapper.CategoryMapper;
import com.serivice.BookService;
import com.serivice.CategoryService;
import com.vo.R;
import com.vo.po.Book;
import com.vo.po.Category;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 42
 * @date 2025/6/5
 * @Description
 */
@Service
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    private final BookService bookService;

    /**
     * 获得图书分类
     * @return
     */
    @Override
    public R<List<Category>> bookCategory() {

        List<Category> list = lambdaQuery().list();
        return R.ok(list);
    }

    @Override
    public R delCategory(Integer id) {

        Integer count = bookService.lambdaQuery().eq(Book::getCategoryId, id).count();
        if(count>0){
            return R.error("该分类下有图书");
        }
        removeById(id);
        return R.ok("删除成功");
    }

    /**
     * 修改分类
     * @param category
     */
    @Override
    public R updateCategory(Category category) {
        updateById(category);
        return R.ok();
    }

    /**
     * 新增分类
     * @param category
     */
    @Override
    public R addCategory(Category category) {
        save(category);
        return R.ok();
    }
}
